package com.alogic.swagger.core;

import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsArray;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import org.apache.commons.lang3.StringUtils;

/**
 * parameter
 *
 * @since 1.6.13.7 [20200812 duanyy] <br>
 */
public class SwaggerParameter extends Segment {
    private String $ref;
    private String $name;
    private String $in = "query";
    private String $desc;
    private String $required;
    private String $deprecated;
    private String $allowEmptyValue;

    private String $style;
    private String $explode;
    private String $allowReserved;
    private String $examples;
    private boolean asArray = true;

    public SwaggerParameter(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties p){
        super.configure(p);
        $ref = PropertiesConstants.getRaw(p,"ref",$ref);
        $name = PropertiesConstants.getRaw(p,"name",$name);
        $in = PropertiesConstants.getRaw(p,"in",$in);
        $desc = PropertiesConstants.getRaw(p,"description",$desc);
        $required = PropertiesConstants.getRaw(p,"required",$required);
        $deprecated = PropertiesConstants.getRaw(p,"deprecated",$deprecated);
        $allowEmptyValue = PropertiesConstants.getRaw(p,"allowEmptyValue",$allowEmptyValue);
        $style = PropertiesConstants.getRaw(p,"style",$style);
        $explode = PropertiesConstants.getRaw(p,"explode",$explode);
        $allowReserved = PropertiesConstants.getRaw(p,"allowReserved",$allowReserved);
        $examples = PropertiesConstants.getRaw(p,"examples",$examples);
        asArray = PropertiesConstants.getBoolean(p,"asArray",true);
    }

    @Override
    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        if (asArray) {
            XsArray array = current.getArrayChild("parameters", true);
            if (array != null) {
                XsObject newObject = array.newObject();
                String ref = PropertiesConstants.transform(ctx, $ref, "");
                if (StringUtils.isEmpty(ref)) {
                    newObject.addProperty("name", PropertiesConstants.transform(ctx, $name, ""));
                    newObject.addProperty("in", PropertiesConstants.transform(ctx, $in, "query"));
                    newObject.addProperty("description", PropertiesConstants.transform(ctx, $desc, ""));
                    if (StringUtils.isNotEmpty(PropertiesConstants.transform(ctx, $required, ""))) {
                        newObject.addProperty("required", PropertiesConstants.transform(ctx, $required, true));
                    }
                    if (StringUtils.isNotEmpty(PropertiesConstants.transform(ctx, $deprecated, ""))) {
                        newObject.addProperty("deprecated", PropertiesConstants.transform(ctx, $deprecated, false));
                    }
                    if (StringUtils.isNotEmpty(PropertiesConstants.transform(ctx, $allowEmptyValue, ""))) {
                        newObject.addProperty("allowEmptyValue", PropertiesConstants.transform(ctx, $allowEmptyValue, false));
                    }
                    newObject.addProperty("style", PropertiesConstants.transform(ctx, $style, ""));
                    newObject.addProperty("explode", PropertiesConstants.transform(ctx, $explode, ""));
                    newObject.addProperty("allowReserved", PropertiesConstants.transform(ctx, $allowReserved, ""));
                    newObject.addProperty("examples", PropertiesConstants.transform(ctx, $examples, ""));
                    super.onExecute(root, newObject, ctx, watcher);
                } else {
                    newObject.addProperty("$ref", ref);
                }
                array.add(newObject);
            }
        }else{
            XsObject asObject = current.getObjectChild("parameters", true);
            if (asObject != null) {
                String name = PropertiesConstants.transform(ctx,$name,"");
                if (StringUtils.isNotEmpty(name)){
                    XsObject newObject = asObject.getObjectChild(name,true);
                    String ref = PropertiesConstants.transform(ctx, $ref, "");
                    if (StringUtils.isEmpty(ref)) {
                        newObject.addProperty("name", name);
                        newObject.addProperty("in", PropertiesConstants.transform(ctx, $in, "query"));
                        newObject.addProperty("description", PropertiesConstants.transform(ctx, $desc, ""));
                        if (StringUtils.isNotEmpty(PropertiesConstants.transform(ctx, $required, ""))) {
                            newObject.addProperty("required", PropertiesConstants.transform(ctx, $required, true));
                        }
                        if (StringUtils.isNotEmpty(PropertiesConstants.transform(ctx, $deprecated, ""))) {
                            newObject.addProperty("deprecated", PropertiesConstants.transform(ctx, $deprecated, false));
                        }
                        if (StringUtils.isNotEmpty(PropertiesConstants.transform(ctx, $allowEmptyValue, ""))) {
                            newObject.addProperty("allowEmptyValue", PropertiesConstants.transform(ctx, $allowEmptyValue, false));
                        }
                        newObject.addProperty("style", PropertiesConstants.transform(ctx, $style, ""));
                        newObject.addProperty("explode", PropertiesConstants.transform(ctx, $explode, ""));
                        newObject.addProperty("allowReserved", PropertiesConstants.transform(ctx, $allowReserved, ""));
                        newObject.addProperty("examples", PropertiesConstants.transform(ctx, $examples, ""));
                        super.onExecute(root, newObject, ctx, watcher);
                    } else {
                        newObject.addProperty("$ref", ref);
                    }
                }
            }
        }
    }
}